home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap18 / Emf4 / Emf4.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.4 KB  |  111 lines

  1. /*-------------------------------------
  2.    EMF4.C -- Enhanced Metafile Demo #4
  3.              (c) Charles Petzold, 1998
  4.   -------------------------------------*/
  5.  
  6. #define OEMRESOURCE
  7. #include <windows.h>
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  12.                     PSTR szCmdLine, int iCmdShow)
  13. {
  14.      static TCHAR szAppName[] = TEXT ("EMF4") ;
  15.      HWND         hwnd ;
  16.      MSG          msg ;
  17.      WNDCLASS     wndclass ;
  18.      
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("Enhanced Metafile Demo #4"),
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.      
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.      
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.      {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.      }
  51.      return msg.wParam ;
  52. }
  53.  
  54. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  55. {
  56.      BITMAP       bm ;
  57.      HBITMAP      hbm ;
  58.      HDC          hdc, hdcEMF, hdcMem ;
  59.      HENHMETAFILE hemf ;
  60.      PAINTSTRUCT  ps ;
  61.      RECT         rect ;
  62.      
  63.      switch (message)
  64.      {
  65.      case WM_CREATE:
  66.           hdcEMF = CreateEnhMetaFile (NULL, TEXT ("emf4.emf"), NULL,
  67.                                       TEXT ("EMF4\0EMF Demo #4\0")) ;
  68.           
  69.           hbm = LoadBitmap (NULL, MAKEINTRESOURCE (OBM_CLOSE)) ;
  70.           
  71.           GetObject (hbm, sizeof (BITMAP), &bm) ;
  72.  
  73.           hdcMem = CreateCompatibleDC (hdcEMF) ;
  74.           
  75.           SelectObject (hdcMem, hbm) ;
  76.           
  77.           StretchBlt (hdcEMF, 100, 100, 100, 100,
  78.                       hdcMem,   0,   0, bm.bmWidth, bm.bmHeight, SRCCOPY) ;
  79.           
  80.           DeleteDC (hdcMem) ;
  81.           DeleteObject (hbm) ;
  82.           
  83.           hemf = CloseEnhMetaFile (hdcEMF) ;
  84.           
  85.           DeleteEnhMetaFile (hemf) ;
  86.           return 0 ;
  87.           
  88.      case WM_PAINT:
  89.           hdc = BeginPaint (hwnd, &ps) ;
  90.           
  91.           GetClientRect (hwnd, &rect) ;
  92.           
  93.           rect.left   =     rect.right  / 4 ;
  94.           rect.right  = 3 * rect.right  / 4 ;
  95.           rect.top    =     rect.bottom / 4 ;
  96.           rect.bottom = 3 * rect.bottom / 4 ;
  97.           
  98.           hemf = GetEnhMetaFile (TEXT ("emf4.emf")) ;
  99.           
  100.           PlayEnhMetaFile (hdc, hemf, &rect) ;
  101.           DeleteEnhMetaFile (hemf) ;
  102.           EndPaint (hwnd, &ps) ;
  103.           return 0 ;
  104.           
  105.      case WM_DESTROY:
  106.           PostQuitMessage (0) ;
  107.           return 0 ;
  108.      }
  109.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  110. }
  111.